package com.bstek.frame.model;

import java.io.Serializable;

/**
 *  RestFul 输出结果的封装
 *
 * @author hans
 */
public class ApiResult<T> implements Serializable {

    private int code;
    private String msg;
    private T data;


    private ApiResult(T data) {
        this.data = data;
    }

    public ApiResult(){

    }
    public ApiResult(int code, String msg) {
        this.code = code;
        this.msg = msg;
    }

    public ApiResult(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * 成功时
     *
     * @return
     */
    public static <T> ApiResult<T> success() {
        return new ApiResult<T>(200,"success");
    }

    /**
     * 成功时
     *
     * @param <T> 数据
     * @return
     */
    public static <T> ApiResult<T> success(T data) {
        return new ApiResult<T>(200,"success",data);
    }

    /**
     * 失败
     *
     * @param msg  应答消息
     * @return
     */
    public static <T> ApiResult<T> fail(String msg) {
        return new ApiResult<T>(400, msg);
    }

    /**
     * 失败
     *
     * @param code 消息码
     * @param msg  应答消息
     * @return
     */
    public static <T> ApiResult<T> fail(int code, String msg) {
        return new ApiResult<T>(code, msg);
    }

    public int getCode() {
        return code;
    }

    public String getMsg() {
        return msg;
    }

    public T getData() {
        return data;
    }


}
